package com.java.test.demo.thread;


import java.util.concurrent.*;

public class MyThreadPoolDemo {
    public static void main(String[] args){
        ExecutorService executorService=new ThreadPoolExecutor(
                2,
                5,
                1L,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<Runnable>(3),
                Executors.defaultThreadFactory(),
//                new ThreadPoolExecutor.AbortPolicy());
//                new ThreadPoolExecutor.CallerRunsPolicy());
//                new ThreadPoolExecutor.DiscardOldestPolicy());
                new ThreadPoolExecutor.DiscardPolicy());
        System.out.println(Runtime.getRuntime().availableProcessors());
        try {
            for (int i = 1; i <=9; i++) {
                executorService.execute(()->{
                    System.out.println(Thread.currentThread().getName()+"\t 执行任务");
                });
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            executorService.shutdown();
        }
    }

    private static void threadPoolInit() {
        //Array Arrays
        //collection Collections
        //Executor Executors
//        ExecutorService executorService= Executors.newFixedThreadPool(5);//一池5个处理线程
//        ExecutorService executorService= Executors.newSingleThreadExecutor();//一池1个处理线程
        ExecutorService executorService= Executors.newCachedThreadPool();//一池x个处理线程

        try {
            for (int i = 0; i < 11; i++) {
                executorService.execute(()->{
                    System.out.println(Thread.currentThread().getName()+"\t 执行任务");
                });
                TimeUnit.SECONDS.sleep(1);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            executorService.shutdown();
        }
    }
}
